package com.example.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.plan.common.result.Result;
import com.example.plan.exception.BusinessException;
import com.example.plan.model.constnant.DroneEnums;
import com.example.plan.model.dto.drone.DroneDTO;
import com.example.plan.model.dto.drone.DronePageDTO;
import com.example.plan.model.entity.ArNavigation;
import com.example.plan.model.entity.Drone;
import com.example.plan.model.vo.ArNavigationVO;
import com.example.plan.model.vo.DroneVO;
import com.example.plan.service.DroneService;
import com.example.plan.mapper.DroneMapper;
import com.example.plan.utils.BeanPageUtil;
import org.springframework.stereotype.Service;

import static com.example.plan.exception.ErrorCode.DRONE_NOT_FOUND;

/**
* @author ASUS
* @description 针对表【drone(无人机设备表)】的数据库操作Service实现
* @createDate 2025-03-01 19:07:17
*/
@Service
public class DroneServiceImpl extends ServiceImpl<DroneMapper, Drone> implements DroneService{

    /**
     * 新增无人机设备
     * @param dto
     * @return
     */
    @Override
    public Result<Void> saveDrone(DroneDTO dto) {
        Drone drone= BeanUtil.copyProperties(dto, Drone.class);
        return baseMapper.insert(drone)>0?Result.success("保存成功"):Result.failed("保存失败");
    }

    /**
     * 删除无人机设备
     * @param droneId
     * @return
     */
    @Override
    public Result<Void> deleteDrone(Long droneId) {
        boolean flag=baseMapper.selectById(droneId)==null||baseMapper.deleteById(droneId)>0;
        return flag?Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     * 无人机设备详情
     * @return
     */
    @Override
    public Result<DroneVO> getDrone(Long droneId) {
        Drone drone=baseMapper.selectById(droneId);
        if(drone==null){
            throw new BusinessException(DRONE_NOT_FOUND);
        }
        DroneVO droneVO=BeanUtil.copyProperties(drone,DroneVO.class);
        return Result.success("无人机设备详情",droneVO);
    }

    /**
     * 无人机设备分页
     * @param dto
     * @return
     */
    @Override
    public Result<IPage<DroneVO>> pageDrone(DronePageDTO dto) {
        QueryWrapper<Drone> queryWrapper=new QueryWrapper<>();
        if (dto.getSortOrder()== DroneEnums.sortOrderEnum.ASC.getCode()){
            queryWrapper.orderByAsc(StrUtil.isNotBlank(dto.getSortField()),dto.getSortField());
        }else {
            queryWrapper.orderByDesc(StrUtil.isNotBlank(dto.getSortField()),dto.getSortField());
        }
        LambdaQueryWrapper<Drone> lambdaQueryWrapper=queryWrapper
                .lambda()
                .eq(dto.getIs_waterproof()!=null,Drone::getIs_waterproof,dto.getIs_waterproof());
        Page<Drone> page = new Page<>(dto.getCurrent(), dto.getPageSize());
        Page<Drone> Drone = baseMapper.selectPage(page, lambdaQueryWrapper);
        Page<DroneVO> pagevo = BeanPageUtil.copyBean(Drone, DroneVO.class);
        return Result.success(pagevo);
    }

    @Override
    public Result<Void> updateDrone(DroneDTO dto) {
        if (dto.getDrone_id()==null){
            throw new BusinessException(DRONE_NOT_FOUND);
        }
        Drone drone= BeanUtil.copyProperties(dto, Drone.class);
        return baseMapper.updateById(drone)>0?Result.success("修改成功"):Result.failed("修改失败");
    }
}




